#include "publiccontact.h"
#include "netim.h"

QVector<PublicContact *> PublicContact::listado;

void PublicContact::ManageConection(QTcpSocket *connection)
{
    int i;
    QHostAddress dir = connection->peerAddress();
    for (i = 0; i < PublicContact::listado.size(); i++) {
        PublicContact *actual = PublicContact::listado.at(i);
        if ((actual->getIp().toString() == dir.toString()))
        {
            if (actual->conversationwindow == NULL) {
                actual->conversationSocket = connection;
                actual->conversationwindow = new ConversationWindow::ConversationWindow(actual,
                        new ConversationConector(actual->conversationSocket));
                actual->conversationwindow->show();
		return;
            } else {
                actual->conversationSocket = connection;
                actual->conversationwindow->updated(new ConversationConector(actual->conversationSocket));
                actual->conversationwindow->show();
                actual->conversationwindow->setEnabled(true);
                return;
            }
        }
    }
    connection->close();
    delete connection;
}

PublicContact::PublicContact()
{
    PublicContact::listado.append(this);
    this->alias = "ERROR!!!";
    this->port = 0;
    this->ip = QHostAddress();
    this->conversationwindow = NULL;
}

PublicContact::PublicContact(QString alias, int port, QHostAddress ip)
{
    PublicContact::listado.append(this);
    this->alias = alias;
    this->port = port;
    this->ip = ip;
    this->inactive_time = 0;
    this->conversationwindow = NULL;
}

PublicContact::~PublicContact()
{
   int i;
   for (i = 0; i < PublicContact::listado.size(); i++)
   {
       if (PublicContact::listado.at(i)->getInactiveTime() > 3)
           PublicContact::listado.remove(i--);
   }
}

QString PublicContact::getAlias()
{
    return this->alias;
}

QHostAddress PublicContact::getIp()
{
    return this->ip;
}

int PublicContact::getPort()
{
    return this->port;
}

int PublicContact::getInactiveTime()
{
    return this->inactive_time;
}

void PublicContact::startConversation()
{
    if (this->conversationwindow == NULL) {
        this->conversationSocket = new QTcpSocket::QTcpSocket();
        this->conversationSocket->connectToHost(this->getIp(), this->getPort());
        this->conversationwindow = new ConversationWindow::ConversationWindow(this,
                 new ConversationConector(this->conversationSocket));
        this->conversationwindow->show();
    }
}

int PublicContact::restartConversation()
{
    if (netim::mode == 2) { // check if visible
        this->conversationSocket->close();
        this->conversationSocket = new QTcpSocket::QTcpSocket();
        this->conversationSocket->connectToHost(this->getIp(), this->getPort());
        this->conversationwindow->updated(
            new ConversationConector(this->conversationSocket));
        return 0;
    } else {
        return -1;
    }
}

void PublicContact::conversationEnded()
{
    delete this->conversationwindow;
    this->conversationwindow = NULL;
    this->conversationSocket->close();
    delete this->conversationSocket;
}

PublicContact PublicContact::newIdentityPacket(QString alias)
{
    this->inactive_time = 0;
    this->alias = alias;
    return *this;
}

PublicContact PublicContact::update()
{
    this->inactive_time++;
    if (this->conversationwindow != NULL) {
        if (this->inactive_time > 3) {
            this->conversationwindow->setEnabled(false);
            if ((this->conversationSocket != NULL) &&
                (this->conversationSocket->state() == QAbstractSocket::ConnectedState))
                this->conversationSocket->close();
        }
        else this->conversationwindow->setEnabled(true);
    }
    return *this;
}

QString PublicContact::getNick()
{
    return this->alias;
}

